﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecutionOrder(-1000)]
public class ResetTransform : MonoBehaviour {

#if UNITY_EDITOR
	bool isInit = false;
#endif
	public Vector2 anchorMin;
	public Vector2 anchorMax;
	public Vector2 pivot;
	public Vector2 sizeDelta;
	public Vector3 position;
	public Vector3 localEulerAngles;
	public Vector3 localScale;
	public bool isActive;

	private void Awake()
	{
		Debug.LogError(this.GetType());
		var tr = transform as RectTransform;
		if(tr!=null)
		{
            tr.anchorMin = anchorMin;
            tr.anchorMax = anchorMax;
            tr.pivot = pivot;
            tr.sizeDelta = sizeDelta;
			tr.anchoredPosition3D = position;
		}
		else
		{
			transform. position = position;
		}
		transform.localEulerAngles = localEulerAngles;
		transform.localScale = localScale;
		gameObject.SetActive(isActive);
	}

#if UNITY_EDITOR
	private void Reset()
	{
		if (isInit)
			return;
		isInit = true;
		UpdateTransform();	
	}

	public void UpdateTransform()
	{
		var tr = transform as RectTransform;
		if (tr != null)
		{
			anchorMin = tr.anchorMin;
			anchorMax = tr.anchorMax;
			pivot = tr.pivot;
			sizeDelta = tr.sizeDelta;
			position = tr.anchoredPosition3D;
		}
		else
		{
			position = transform.position;
		}

		localEulerAngles = transform.localEulerAngles;
		localScale = transform.localScale;
		isActive = gameObject.activeInHierarchy;
	}
#endif
}
